/*
ID: icerupt1
PROG: spin
LANG: C++11
*/

/* solution
 *
 *
*/
#include <fstream>
#include <iostream>
#include <vector>

std::ifstream fin {"spin.in" };
std::ofstream fout{"spin.out"};

struct wedge { int start, extent; };

std::vector<std::vector<wedge>> steel(5);
std::vector<int> speed(5);

void rotate()
{
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < (int)steel[i].size(); j++)
			steel[i][j].start = (steel[i][j].start + speed[i]) % 360;
	}
}

bool light_through(int d)
{
	for (int i = 0; i < 5; i++) {
		bool can_pass = false;
		for (int j = 0; j < (int)steel[i].size(); j++) {
			int start = steel[i][j].start % 360;
			int end = steel[i][j].extent + start;
			int tmp = d;
			if (tmp < start) tmp += 360;
			if (start <= tmp && tmp <= end) { can_pass = true; break; }
		}
		if (!can_pass) return false;
	}
	return true;
}

bool check()
{
	for (int degree = 0; degree < 360; degree++)
		if (light_through(degree)) return true;
	return false;
}

int main()
{
	for (int i = 0, t; i < 5; i++) {
		fin >> speed[i] >> t;
		steel[i].resize(t);
		for (int j = 0; j < t; j++)
			fin >> steel[i][j].start >> steel[i][j].extent;
	}

	if (check()) { std::cout << "0\n"; fout << "0\n"; return 0; }

	int max_time = 10000;
	for (int time = 1; time < max_time; time++) {
		rotate();
		if (check()) {
			std::cout << time << '\n';
			fout << time << '\n';
			return 0;
		}
	}
	std::cout << "none\n";
	fout << "none\n";
}

